<template>
  <div class="search-sort">
    <ul class="sort">
      <li @click="searchSort(null)" :class="{ active: search.sortField === null }">默认排序</li>
      <li @click="searchSort('publishTime')" :class="{ active: search.sortField === 'publishTime' }">最新商品</li>
      <li @click="searchSort('orderNum')" :class="{ active: search.sortField === 'orderNum' }">最高人气</li>
      <li @click="searchSort('evaluateNum')" :class="{ active: search.sortField === 'evaluateNum' }">评论最多</li>
      <li @click="searchSort('price')">
        价格排序
        <i class="arrow up" :class="{ active: search.sortMethod === 'asc' }" />
        <i class="arrow down" :class="{ active: search.sortMethod === 'desc' }" />
      </li>
    </ul>
  </div>
</template>
<script>
import { reactive } from 'vue'
import { onBeforeRouteUpdate } from 'vue-router'
export default {
  name: 'SeachSort',
  setup(prop, { emit }) {
    // 切换效果
    // 1.查看后台数据排序约定
    // sortField的参数：publishTime：最新商品, orderNum：最高人气, evaluateNum：评论最多,  price:价格
    // 1.1 如果点击了价格需要传额外字段：sortMethod
    // 1.2 sortMethod有两个值：asc为正序，desc为倒序（默认值）
    // 默认排序 sortField为null即可，不是点的价格sortMethod也为null，（后台不需要的参数传null即可）
    const search = reactive({
      sortField: null,
      sortMethod: null
    })
    // 搜索关键子改变触发
    onBeforeRouteUpdate((to, from, nex) => {
      search.sortField = null
      search.sortMethod = null
      nex()
    })
    // 点击排序触发
    const searchSort = sortField => {
      // 判斷点击的是否是价格
      if (sortField === 'price') {
        // 判断是不是第一次点击价格
        if (search.sortMethod === null) {
          search.sortField = sortField
          search.sortMethod = 'desc'
        } else {
          search.sortField = sortField
          search.sortMethod = search.sortMethod === 'desc' ? 'asc' : 'desc'
        }
      } else {
        // 判断是否重复点击同一个高亮
        if (search.sortField === sortField) return
        search.sortField = sortField
        search.sortMethod = null
      }
      // 通知父组件改变排序方式
      emit('change-filter', search)
    }
    return { search, searchSort }
  }
}
</script>
<style lang="less" scoped>
.search-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    li {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all 0.3s;
      cursor: pointer;
      &.active {
        background: #27ba9b;
        color: #fff;
        border-color: #27ba9b;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 5px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
          &.active {
            border-bottom-color: var(--xtx-color);
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: var(--xtx-color);
          }
        }
      }
    }
  }
}
</style>
